package results;

import com.hp.gagawa.java.elements.*;

import static queped.NeptuneHelperFunctions.*;
import static results.ResultsTableFactory.*;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.*;
import java.util.HashMap;
import java.util.List;

/**
 * Created by:  Jason Ye
 * Date:        23/06/2012
 * Time:        14:14
 */
public class JSimResultsDisplayer {

    HashMap<String, HashMap<String, HashMap<String, String>>> results = new HashMap<String, HashMap<String, HashMap<String, String>>>();

    HashMap<String, HashMap<String, String>> systemResults = new HashMap<String, HashMap<String, String>>();

    Html html = new Html();

    public JSimResultsDisplayer(String resultsFile) {

        Div topLevel = new Div();
        H1 heading = new H1();
        heading.appendText("QuePED++ Queueing Network Results for Results File " + resultsFile);
        topLevel.appendChild(heading);

        Table table = newTableSpecificResultsTable();
        table.setBorder("5");

        /** debug **/

        SAXBuilder builder = new SAXBuilder();
        //File xmlFile = new File("temp/res_sim_QN120120624172736.jsimg.xml");
        File xmlFile = new File(resultsFile);

        try {

            Document document = builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren();


            for (int i = 0; i < list.size(); i++) {

                Element node = (Element) list.get(i);

                String stationName = node.getAttribute("station").getValue();
                String meanvalue = node.getAttribute("meanValue").getValue();
                String alpha = node.getAttribute("alfa").getValue();
                String measureType = node.getAttribute("measureType").getValue();
                String precision = node.getAttribute("precision").getValue();
                String transaction = node.getAttribute("class").getValue();

                if (measureType.startsWith("System ")) {
                    HashMap<String, String> transactionSystemResults = componentExtract(systemResults, transaction, new HashMap<String, String>());
                    transactionSystemResults.put(measureType, meanvalue);
                } else {

                    // get correct container for results for table
                    HashMap<String, HashMap<String, String>> resultsForTable = componentExtract(results, stationName, new HashMap<String, HashMap<String, String>>());
                    // get correct container for transaction specific results

                    HashMap<String, String> resultsForTransaction = componentExtract(resultsForTable, transaction, new HashMap<String, String>());

                    // put in correct transaction/table combo
                    resultsForTransaction.put(measureType, meanvalue);
                }
            }

            results.put("System", systemResults);

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }

        /** debug **/


        for (String relation : results.keySet()) {

            if (relation != null & !relation.equals("") && !relation.equals("System")) {

                HashMap<String, HashMap<String, String>> resultsForRelation = results.get(relation);

                table = newResultsForRelation(table, relation, resultsForRelation);

            }

        }


        topLevel.appendChild(table);

        Table systemResultsTable = systemResultsTable();

        for (String relation : systemResults.keySet()) {

            HashMap<String, String> systemResultsForRelation = systemResults.get(relation);

            systemResultsTable = newSystemResultsForRelation(systemResultsTable, relation, systemResultsForRelation);
        }
        topLevel.appendChild(new Br());
        topLevel.appendChild(systemResultsTable);
        html.appendChild(topLevel);


        if(isInDebugMode()) System.out.print(html.write());
    }

    public void saveHTMLFile(String resultsHTMLFileName) {

        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(resultsHTMLFileName));
            out.write(html.write());
            out.close();
        } catch (IOException e) {
            System.out.println("Exception ");

        }
    }

    public static void main(String args[]) {

        JSimResultsDisplayer sib = new JSimResultsDisplayer("temp/res_sim_QN120120624172736.jsimg.xml");


    }
}
